home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Abalone 1.4.2 / src / AEhandler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-21  |  22.7 KB  |  1,021 lines  |  [TEXT/MPS ]

  1. #define AEHANDLER_C
  2. #include "AEHandler.h"
  3.  
  4.  
  5. #if defined(__MWERKS__)
  6. #pragma segment __%Main
  7. #else
  8. #pragma segment Main
  9. #endif
  10.  
  11.  
  12. #define    kReplyPref    (kAEWaitReply | kAEAlwaysInteract | kAECanSwitchLayer)
  13. #define kTimeOut    200
  14.  
  15.  
  16. AEIdleUPP    idleUPP = 0;
  17.  
  18. OSErr
  19. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  20. AEOpenApplicationHandler (AppleEvent * message, long refCon)
  21. #else
  22. AEOpenApplicationHandler (AppleEvent *, long)
  23. #endif
  24. {
  25.     InitRules();
  26.     InitGame();
  27.     ShowWindow (gAbaloneWindow);
  28.     return noErr;
  29. }
  30.  
  31.  
  32.  
  33. OSErr
  34. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  35. AEOpenDocumentsHandler (AppleEvent *message, long refCon)
  36. #else
  37. AEOpenDocumentsHandler (AppleEvent *message, long)
  38. #endif
  39. {
  40.     OSErr        error;
  41.     AEKeyword    keyword;
  42.     DescType    realType;
  43.     AEDescList    docList;
  44.     long        items;
  45.     Size        realSize;
  46.     FSSpec        specification;
  47.         
  48.     error = AEGetParamDesc (message, keyDirectObject, typeAEList, & docList);
  49.  
  50.     if (error == noErr)
  51.     {    
  52.         error = AECountItems (& docList, & items);
  53.         
  54.         if (items > 1 && error == noErr)
  55.             Warning (ONE_FILE_ONLY);
  56.     }
  57.     if (error == noErr)
  58.         error = AEGetNthPtr (& docList, 1, typeFSS, & keyword, & realType, (Ptr) & specification, sizeof (FSSpec), & realSize);
  59.         
  60. //    In case an Open Documents event is generated, the Open Application event is NOT,
  61. //    (this WAS a real surprise to me)
  62. //    so the same code as in AEOpenApplicationHandler should appear here as well
  63.         
  64.     InitRules();
  65.     
  66.     if (error != noErr)
  67.         InitGame();
  68.     else
  69.         OpenGameSpec (& specification);
  70.         
  71.     ShowWindow (gAbaloneWindow);
  72.     
  73.     AEDisposeDesc (& docList);
  74.     
  75.     return noErr;
  76. }
  77.  
  78.  
  79.  
  80. OSErr
  81. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  82. AEPrintDocumentsHandler (AppleEvent *message, long refCon)
  83. #else
  84. AEPrintDocumentsHandler (AppleEvent *, long)
  85. #endif
  86. {
  87.     Warning (PRINTING_NOT_IMPLEMENTED);
  88.     return noErr;
  89. }
  90.  
  91.  
  92.  
  93. OSErr
  94. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  95. AEQuitApplicationHandler (AppleEvent *message, long refCon)
  96. #else
  97. AEQuitApplicationHandler (AppleEvent *, long)
  98. #endif
  99. {
  100.     Terminate();
  101.     return noErr;
  102. }
  103.  
  104.  
  105.  
  106. OSErr
  107. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  108. AbaloneMoveHandler (AppleEvent *message, AppleEvent *reply, long refCon)
  109. #else
  110. AbaloneMoveHandler (AppleEvent *message, AppleEvent *, long)
  111. #endif
  112. {
  113.     OSErr        error;
  114.     MoveData    move;
  115.     long        chck;
  116.     short        plyr;
  117.     DescType    realType;
  118.     Size        realSize;
  119.  
  120.     error = AEGetParamPtr (message, kSendMove, typeMove, & realType, (Ptr) & move, sizeof (long), & realSize);
  121.     if (error == noErr)
  122.     {
  123. //    Assert (realType == typeMove, INTERNAL_ERROR);
  124.     error = AEGetParamPtr (message, kSendPlyr, typePlyr, & realType, (Ptr) & plyr, sizeof (short), & realSize);
  125.     }
  126.     if (error == noErr)
  127.     {
  128. //    Assert (realType == typePlyr, INTERNAL_ERROR);
  129.     error = AEGetParamPtr (message, kSendChck, typeChck, & realType, (Ptr) & chck, sizeof (long), & realSize);
  130.     }
  131.     if (error == noErr)
  132.     {
  133. //    Assert (realType == typeChck, INTERNAL_ERROR);
  134.     }
  135.     if (move == gLastMove)
  136.     {
  137.         return duplicateMoveErr;
  138.     }
  139.     if (chck != CheckSum (CurrentBoard()) && chck != CheckSum (RecentBoard()))
  140.     {
  141.         return outOfSyncErr;
  142.     }
  143.     if (! ProcessMove (move, true))
  144.     {
  145.         return illegalMoveErr;
  146.     }
  147.     return error;
  148. }
  149.  
  150.  
  151.  
  152.  
  153. OSErr
  154. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  155. AbaloneGameHandler (AppleEvent *message, AppleEvent *reply, long refCon)
  156. #else
  157. AbaloneGameHandler (AppleEvent *message, AppleEvent *reply, long)
  158. #endif
  159. {
  160.     OSErr        error;
  161.     DescType    realType;
  162.     Size        realSize;
  163.     Game        otherGame;
  164.     Board        otherBoard;
  165.     
  166.     
  167.     error = AEGetParamPtr    (    message,
  168.                                 kSendGame,
  169.                                 typeGame,
  170.                                 & realType,
  171.                                 (Ptr) & otherGame,
  172.                                 sizeof (Game),
  173.                                 & realSize
  174.                             );
  175.     if (realType != typeGame || realSize != sizeof (Game))
  176.     {
  177.         return missingDataErr;
  178.     }
  179.     error = AEGetParamPtr    (    message,
  180.                                 kSendBord,
  181.                                 typeBord,
  182.                                 & realType,
  183.                                 (Ptr) & otherBoard,
  184.                                 sizeof (Board),
  185.                                 & realSize
  186.                             );
  187.     if (realType != typeBord || realSize != sizeof (Board))
  188.     {
  189.         return missingDataErr;
  190.     }
  191.     if ( CheckSum (& otherBoard) != CheckSum (CurrentBoard()))
  192.     {
  193.         if (otherGame.CurrentMove < gTheGame.CurrentMove)
  194.         {
  195.             Warning (OUT_OF_SYNC);
  196.             
  197.             gTheGame = otherGame;
  198.             CopyBoard (& otherBoard, CurrentBoard());
  199.             gTheGame.Board = CurrentBoard();
  200.             InvalBoard();
  201.         }
  202.     //    else: the other side will do this
  203.     }
  204.     error = AEPutParamPtr (reply, kReplyBord, typeBord, (Ptr) CurrentBoard(), sizeof (Board));
  205.     error = AEPutParamPtr (reply, kReplyGame, typeGame, (Ptr) & gTheGame, sizeof (Game));
  206.  
  207.     return error;
  208. }
  209.  
  210.  
  211.  
  212. //    The init handler takes care of the connection protocol.
  213. //    The sender (the client) puts its settings in the message sent;
  214. //    The receiver (the server) should use these settings to do the following:
  215. //    - extract the senders address
  216. //    - compute the settings the client should have for a successfull connection
  217. //    - put the requested settings in the reply message
  218. //    
  219. //    The client will (or will not) receive the reply message.
  220. //    With the reply message, it should do the following:
  221. //    - extract the requested settings
  222. //    - set its settings as requested (or send a 'quit' event to cancel).
  223.  
  224.  
  225. OSErr
  226. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  227. AbaloneInitHandler (AppleEvent *message, AppleEvent *reply, long refCon)
  228. #else
  229. AbaloneInitHandler (AppleEvent *message, AppleEvent *reply, long)
  230. #endif
  231. {
  232.     OSErr            error;
  233.     AEAddressDesc    clientAddress;
  234.     DescType        realType;
  235.     Size            realSize;
  236.     Settings        otherSettings;
  237.     short            p;
  238.     long            checkSum;
  239.     
  240.     error = AEGetParamPtr    (    message,
  241.                                 kSendSets,
  242.                                 typeSets,
  243.                                 & realType,
  244.                                 (Ptr) & otherSettings,
  245.                                 sizeof (Settings),
  246.                                 & realSize
  247.                             );
  248.     if (realType != typeSets || realSize != sizeof (Settings))
  249.     {
  250.         return missingDataErr;
  251.     }
  252. //    The clients settings are now known.
  253. //    Match them with ours, and change the other sides settings (or ours) when needed.
  254.     
  255.     MatchSettings (& gSet, & otherSettings);
  256.     
  257. //    Get the clients address
  258.     
  259.     if (AEGetAttributeDesc (message, keyAddressAttr, typeWildCard, &clientAddress) != noErr)
  260.     {
  261.         return missingDataErr;
  262.     }
  263.     
  264. //    Add the requested settings to the reply,
  265. //    and assume our opponents does as requested.
  266.  
  267.     error = AEPutParamPtr (reply, kReplySet1, typeSets, (Ptr) & otherSettings, sizeof (Settings));
  268.  
  269. //    Add my own settings to the reply as well.
  270. //    This makes the protocol a bit more symmetric: both parties get a look at both settings.
  271. //    The 'unfair' part is we demand the other party to make almost all the adjustments.
  272.  
  273.     error = AEPutParamPtr (reply, kReplySet2, typeSets, (Ptr) & gSet, sizeof (Settings));
  274.  
  275. //    Add a checksum of the current game as well.
  276. //    If we're out of sync, let the other party adjust his board.
  277. //    Worse yet, let him figure out there's a problem at all.
  278.  
  279.     checkSum = CheckSum (CurrentBoard());
  280.     error = AEPutParamPtr (reply, kReplyChck, typeChck, (Ptr) & checkSum, sizeof (long));
  281.  
  282. //    We have a valid address.
  283. //    Use the clients address for all the players we know are over there.
  284.     
  285.     for (p = blak; p <= gSet.Players; p++)
  286.     {
  287.         if    ( ! IsNetPlayer (& otherSettings, p))
  288.         {
  289.             SetNetAddress (& clientAddress, p);
  290.         }
  291.     }
  292.  
  293.     return noErr;
  294. }
  295.  
  296.  
  297.  
  298. OSErr
  299. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  300. AbaloneNew1Handler (AppleEvent *message, AppleEvent *reply, long refCon)
  301. #else
  302. AbaloneNew1Handler (AppleEvent *message, AppleEvent *reply, long)
  303. #endif
  304. {
  305.     OSErr            error;
  306.     AEAddressDesc    clientAddress;
  307.     DescType        realType;
  308.     Size            realSize;
  309.     Settings        otherSettings;
  310.     short            p, q;
  311.     
  312.     error = AEGetParamPtr    (    message,
  313.                                 kSendSets,
  314.                                 typeSets,
  315.                                 & realType,
  316.                                 (Ptr) & otherSettings,
  317.                                 sizeof (Settings),
  318.                                 & realSize
  319.                             );
  320.     if (error || realType != typeSets || realSize != sizeof (Settings))
  321.     {
  322.         return missingDataErr;
  323.     }
  324.     
  325. //    Get the clients address
  326.     
  327.     if (AEGetAttributeDesc (message, keyAddressAttr, typeWildCard, &clientAddress) != noErr)
  328.     {
  329.         return noConnectionErr;
  330.     }
  331.     
  332. //    Since we're the only one receiving this connection,
  333. //    we should broadcast an update of the net addresses.
  334.  
  335.     for (p = blak; p <= gSet.Players; p++)
  336.     {
  337.         if    (    IsNetPlayer (& gSet, p)                //    player is on the net for us.
  338.             &&    IsConnected (p)                        //    we have a connection
  339.             )
  340.         {
  341.             if (IsNetPlayer (& otherSettings, p))
  342.             {
  343.             //    This is a net player for our client,
  344.             //    so our client will be interested in this address we have.
  345.             
  346.                 SendAEAdrs (& clientAddress, GetNetAddress(p), p);
  347.             }
  348.             for (q = blak; q <= gSet.Players; q++)
  349.             {
  350.             //    This is a net player for us, but not for our client,
  351.             //    so the other net players we know about will be interested.
  352.             
  353.                 if    (    IsNetPlayer (& gSet, q)        //    player is on the net for us.
  354.                     &&    ! IsNetPlayer (& otherSettings, q)    //    our client handles the player
  355.                     &&    p != q)                        //    it's not the same player
  356.                 {
  357.                     SendAEAdrs (GetNetAddress(p), & clientAddress, q);
  358.                 }
  359.             }
  360.         }
  361.     }
  362.  
  363.  
  364. //    Add the requested settings to the reply,
  365. //    and assume our opponents does as requested.
  366.  
  367.     error = AEPutParamPtr (reply, kReplySet1, typeSets, (Ptr) & otherSettings, sizeof (Settings));
  368.  
  369. //    Add my own settings to the reply as well.
  370. //    This makes the protocol a bit more symmetric: both parties get a look at both settings.
  371. //    The 'unfair' part is we demand the other party to make almost all the adjustments.
  372.  
  373.     error = AEPutParamPtr (reply, kReplySet2, typeSets, (Ptr) & gSet, sizeof (Settings));
  374.  
  375. //    Add the current game as well. If we're out of sync, let the other party adjust his board.
  376.  
  377.     error = AEPutParamPtr (reply, kReplyBord, typeBord, (Ptr) CurrentBoard(), sizeof (Board));
  378.     error = AEPutParamPtr (reply, kReplyGame, typeGame, (Ptr) & gTheGame, sizeof (Game));
  379.  
  380.     return noErr;
  381. }
  382.  
  383.  
  384.  
  385. OSErr    
  386. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  387. AbaloneStopHandler (AppleEvent *message, AppleEvent *reply, long refCon)
  388. #else
  389. AbaloneStopHandler (AppleEvent *message, AppleEvent *, long)
  390. #endif
  391. {
  392.     OSErr            error;
  393.     DescType        realType;
  394.     Size            realSize;
  395.     short            quitter;
  396.  
  397. //    Nothing we can do except telling our side the other side has stopped,
  398. //    and clearing the net address for this player of course.
  399.  
  400.     if (gQuitFlag)    //    never mind, we already knew.
  401.         return noErr;
  402.  
  403.  
  404.     error = AEGetParamPtr    (    message,
  405.                                 kSendPlyr,
  406.                                 typePlyr,
  407.                                 & realType,
  408.                                  (Ptr)& quitter,
  409.                                 sizeof (short),
  410.                                 & realSize
  411.                             );
  412.     if (realType != typePlyr || realSize != sizeof (short))
  413.     {
  414.         return missingDataErr;
  415.     }
  416.     
  417. //    Assert (gSet.PlayerKind[quitter] == networkPlayer, INTERNAL_ERROR);
  418.  
  419.     CurrentCursor (arrowCursor);
  420.     switch (Alert (401, nil))
  421.     {
  422.         case 1:    //    Take Over
  423.             ClearNetAddress (quitter);
  424.             SetPlayerKind (& gSet, quitter, macPlayer);
  425.         break;
  426.         case 2:    //    Just Wait
  427.         //    Make sure we really wait by forcing the first player to be a network player
  428.             SetPlayerKind (& gSet, blak, networkPlayer);
  429.         break;
  430.         case 3:    //    Quit Game
  431.             BreakConnections();    //    to make things even worse for a third player
  432.             Terminate();
  433.         break;
  434.     }
  435.     return noErr;
  436. }
  437.  
  438.  
  439.  
  440. OSErr    
  441. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  442. AbaloneAdrsHandler (AppleEvent *message, AppleEvent *reply, long refCon)
  443. #else
  444. AbaloneAdrsHandler (AppleEvent *message, AppleEvent *, long)
  445. #endif
  446. {
  447.     OSErr            error;
  448.     DescType        realType;
  449.     Size            realSize;
  450.     short            player;
  451.     TargetID        addressID;
  452.     AEAddressDesc    addressDesc;
  453.  
  454. //    Get the player ID. This is the player whose address we're about to receive.
  455.  
  456.     error = AEGetParamPtr    (    message,
  457.                                 kSendPlyr,
  458.                                 typePlyr,
  459.                                 & realType,
  460.                                 (Ptr)& player,
  461.                                 sizeof (short),
  462.                                 & realSize
  463.                             );
  464.     if (realType != typePlyr || realSize != sizeof (short))
  465.     {
  466.         return missingDataErr;
  467.     }
  468.     if (! IsNetPlayer (& gSet, player))
  469.     {
  470.     //    Don't bother handling this.
  471.         return error;
  472.     }
  473. //    Get the player address. It's send as a TargetID, so it needs to be converted.
  474.     
  475.     error = AEGetParamPtr    (    message,
  476.                                 kSendAdrs,
  477.                                 typeAdrs,
  478.                                 & realType,
  479.                                 (Ptr)& addressID,
  480.                                 sizeof (TargetID),
  481.                                 & realSize
  482.                             );
  483.     if (realType != typeAdrs || realSize != sizeof (TargetID))
  484.     {
  485.         return missingDataErr;
  486.     }
  487.  
  488.     error = AECreateDesc(
  489.         typeTargetID,            /* Standard target descriptor type. */
  490.         (Ptr)&addressID,        /* The data for the descriptor.        */
  491.         sizeof(TargetID),        /* Size of the data.                */
  492.         &addressDesc            /* Wherefore art thou desc.            */
  493.     );
  494.  
  495.     SetNetAddress (& addressDesc, player);
  496.  
  497.     AEDisposeDesc (& addressDesc);
  498.     
  499.     return error;
  500. }
  501.  
  502.  
  503.  
  504. pascal OSErr
  505. SendAEMove (AEAddressDesc *opponent, MovePtr move, short plyr, long chck)
  506. {
  507.     AppleEvent        moveEvent, moveReply;
  508.     OSErr            error, therror;
  509.     DescType        realType;
  510.     long            realSize;
  511.         
  512. //    *)    IM VI Ch.6 p.19
  513. //    1)    Create using AECreateAppleEvent;
  514. //
  515.     AECreateAppleEvent (    kAbaloneEventClass,
  516.                             kSendMove,
  517.                             opponent,
  518.                             kAutoGenerateReturnID,
  519.                             kAnyTransactionID,
  520.                             & moveEvent
  521.                         );
  522.  
  523. //    2)    Use Apple Event manager functions to add parameters
  524.  
  525.     AEPutParamPtr (& moveEvent, kSendMove, typeMove, (Ptr)   move, sizeof (long));
  526.     AEPutParamPtr (& moveEvent, kSendPlyr, typePlyr, (Ptr) & plyr, sizeof (short));
  527.     AEPutParamPtr (& moveEvent, kSendChck, typeChck, (Ptr) & chck, sizeof (long));
  528.     
  529. //    3)    Call AESend
  530.  
  531.     if (idleUPP == 0)    idleUPP = NewAEIdleProc (IdleTime);
  532.  
  533.     error = AESend    (    & moveEvent,
  534.                         & moveReply,
  535.                         kReplyPref,
  536.                         kAENormalPriority,
  537.                         kTimeOut,
  538.                         (AEIdleUPP) idleUPP,
  539.                         0L
  540.                     );
  541.  
  542. //    5)    Process the reply Apple event
  543.     AEGetParamPtr    (    & moveReply,
  544.                         keyErrorNumber,
  545.                         typeShortInteger,
  546.                         & realType,
  547.                         (Ptr) & therror,
  548.                         sizeof (OSErr),
  549.                         & realSize
  550.                     );
  551.                             
  552. //    4)    Dispose of copies of descriptor records
  553.     
  554.     AEDisposeDesc (& moveEvent);
  555.  
  556. //    6)    Dispose of reply descriptor record
  557.  
  558.     AEDisposeDesc (& moveReply);
  559.     
  560.     return therror != noErr ? therror : error;
  561. }
  562.  
  563.  
  564.  
  565. pascal OSErr
  566. SendAEGame    (    AEAddressDesc *opponent,
  567.                 GamePtr game,
  568. #if defined (__SC__) || defined (THINK_C) || defined(powerc)
  569.                 BoardPtr board,
  570. #else
  571.                 BoardPtr,
  572. #endif
  573.                 GamePtr serverGame,
  574.                 BoardPtr serverBoard
  575.             )
  576. {
  577.     AppleEvent        gameEvent, gameReply;
  578.     OSErr            error;
  579.     DescType        realType;
  580.     long            realSize;
  581.         
  582. //    Create the actual Apple Event.        
  583.     AECreateAppleEvent (    kAbaloneEventClass,
  584.                             kSendGame,
  585.                             opponent,
  586.                             kAutoGenerateReturnID,
  587.                             kAnyTransactionID,
  588.                             & gameEvent
  589.                         );
  590.     AEPutParamPtr (& gameEvent, kSendGame, typeGame, (Ptr) game, sizeof (Game));
  591.     AEPutParamPtr (& gameEvent, kSendBord, typeBord, (Ptr) game->Board, sizeof (Board));
  592.  
  593.     if (idleUPP == 0)    idleUPP = NewAEIdleProc (IdleTime);
  594.     
  595. //    Send the event
  596.     error = AESend    (    & gameEvent,
  597.                         & gameReply,
  598.                         kReplyPref,
  599.                         kAENormalPriority,
  600.                         kTimeOut,
  601.                         (AEIdleUPP) idleUPP,
  602.                         0L
  603.                     );
  604.                             
  605. //    Handle the reply event.
  606.  
  607. //    The server game
  608.     
  609.     error = AEGetParamPtr (& gameReply, kReplyGame, typeGame, & realType, (Ptr) serverGame, sizeof (Game), & realSize);
  610.     if (realSize != sizeof (Game))
  611.     {
  612.         error = missingDataErr;
  613.         goto CleanUpGame;
  614.     }
  615.                             
  616. //    The server board
  617.     
  618.     error = AEGetParamPtr (    & gameReply, kReplyBord, typeBord, & realType, (Ptr) serverBoard, sizeof (Board), & realSize);
  619.     if (realSize != sizeof (Board))
  620.     {
  621.         error = missingDataErr;
  622.         goto CleanUpGame;
  623.     }
  624.  
  625. CleanUpGame:
  626.                             
  627. //    Dispose of copies of descriptor records
  628.  
  629.     AEDisposeDesc (& gameEvent);
  630.  
  631. //    Dispose of reply descriptor record and the parameter list.
  632.  
  633.     AEDisposeDesc (& gameReply);
  634.     
  635.     return error;
  636. }
  637.  
  638.  
  639.  
  640. pascal OSErr
  641. SendAEInit (AEAddressDesc *opponent, SettingsPtr settings, SettingsPtr serverSettings, long *checkSum)
  642. {
  643.     AppleEvent        initEvent, initReply;
  644.     OSErr            error = noErr;
  645.     DescType        realType;
  646.     long            realSize;
  647.  
  648.         
  649. //    Create the actual Apple Event.        
  650.     AECreateAppleEvent (    kAbaloneEventClass,
  651.                             kSendInit,
  652.                             opponent,
  653.                             kAutoGenerateReturnID,
  654.                             kAnyTransactionID,
  655.                             & initEvent
  656.                         );
  657.     
  658. //    Add the settings to the message
  659.     error = AEPutParamPtr (& initEvent, kSendSets, typeSets, (Ptr) settings, sizeof (Settings));
  660.  
  661.     if (idleUPP == 0)    idleUPP = NewAEIdleProc (IdleTime);
  662.     
  663. //    Send the event
  664.     error = AESend    (    & initEvent,
  665.                         & initReply,
  666.                         kReplyPref,
  667.                         kAENormalPriority,
  668.                         kTimeOut,
  669.                         (AEIdleUPP) idleUPP,
  670.                         0L
  671.                     );
  672.  
  673.     if (error != noErr)
  674.     {
  675.         error = noConnectionErr;
  676.         goto CleanupInit;
  677.     }
  678.                     
  679. //    Handle the reply event.
  680.  
  681. //    1.    The requested settings
  682.  
  683.     error = AEGetParamPtr    (    & initReply,
  684.                                 kReplySet1,
  685.                                 typeSets,
  686.                                 & realType,
  687.                                 (Ptr) settings,
  688.                                 sizeof (Settings),
  689.                                 & realSize
  690.                             );
  691.     if (realSize != sizeof (Settings))
  692.     {
  693.         error = missingDataErr;
  694.         goto CleanupInit;
  695.     }
  696.  
  697. //    2.    The server settings
  698.  
  699.     error = AEGetParamPtr    (    & initReply,
  700.                                 kReplySet2,
  701.                                 typeSets,
  702.                                 & realType,
  703.                                 (Ptr) serverSettings,
  704.                                 sizeof (Settings),
  705.                                 & realSize
  706.                             );
  707.     if (realSize != sizeof (Settings))
  708.     {
  709.         error = missingDataErr;
  710.         goto CleanupInit;
  711.     }
  712.  
  713. //    3.    The server board checksum
  714.     
  715.     error = AEGetParamPtr    (    & initReply,
  716.                                 kReplyChck,
  717.                                 typeChck,
  718.                                 & realType,
  719.                                 (Ptr) checkSum,
  720.                                 sizeof (long),
  721.                                 & realSize
  722.                             );
  723.     if (realSize != sizeof (long))
  724.     {
  725.         error = missingDataErr;
  726.     }
  727.                             
  728. //    Dispose of all AE stuff we created
  729.  
  730. CleanupInit:
  731.  
  732.     AEDisposeDesc (& initEvent);
  733.     AEDisposeDesc (& initReply);
  734.     
  735.     return error;
  736. }
  737.  
  738.  
  739.  
  740. pascal OSErr
  741. SendAENew1 (AEAddressDesc *opponent, SettingsPtr settings)
  742. {
  743.     AppleEvent        new1Event, new1Reply;
  744.     OSErr            error = noErr;
  745.  
  746.         
  747. //    Create the actual Apple Event.        
  748.     AECreateAppleEvent (    kAbaloneEventClass,
  749.                             kSendNew1,
  750.                             opponent,
  751.                             kAutoGenerateReturnID,
  752.                             kAnyTransactionID,
  753.                             & new1Event
  754.                         );
  755.     
  756. //    Add the settings to the message
  757.     error = AEPutParamPtr (& new1Event, kSendSets, typeSets, (Ptr) settings, sizeof (Settings));
  758.  
  759.     if (idleUPP == 0)    idleUPP = NewAEIdleProc (IdleTime);
  760.     
  761. //    Send the event
  762.     error = AESend    (    & new1Event,
  763.                         & new1Reply,
  764.                         kReplyPref,
  765.                         kAENormalPriority,
  766.                         kTimeOut,
  767.                         (AEIdleUPP) idleUPP,
  768.                         0L
  769.                     );
  770.  
  771.     if (error != noErr)
  772.     {
  773.         error = noConnectionErr;
  774.     }
  775.                             
  776. //    Dispose of all AE stuff we created
  777.  
  778. CleanupNew1:
  779.  
  780.     AEDisposeDesc (& new1Event);
  781.     AEDisposeDesc (& new1Reply);
  782.     
  783.     return error;
  784. }
  785.  
  786.  
  787.  
  788. pascal OSErr
  789. SendAEStop (AEAddressDesc *opponent, short quitter)
  790. {
  791.  
  792.     AppleEvent        stopEvent, stopReply;
  793.     OSErr            error;
  794.         
  795. //    Create the actual Apple Event.        
  796.     AECreateAppleEvent (    kAbaloneEventClass,
  797.                             kSendStop,
  798.                             opponent,
  799.                             kAutoGenerateReturnID,
  800.                             kAnyTransactionID,
  801.                             & stopEvent
  802.                         );
  803.  
  804. //    Add the quitting player to the message
  805.     error = AEPutParamPtr (& stopEvent, kSendPlyr, typePlyr, (Ptr) & quitter, sizeof (short));
  806.     
  807. //    Send the event
  808.     error = AESend    (    & stopEvent,
  809.                         & stopReply,
  810.                         kAENoReply,            //    Not in this case, thank you
  811.                         kAENormalPriority,
  812.                         kTimeOut,
  813.                         0L, //    (AEIdleProcPtr) IdleTime,
  814.                         0L
  815.                     );
  816.                     
  817.     AEDisposeDesc (& stopReply);
  818.     
  819.     return error;
  820. }
  821.  
  822.  
  823.  
  824. pascal OSErr
  825. SendAEAdrs (AEAddressDesc *opponent, AEAddressDesc *clientAddress, short player)
  826. {
  827.     AppleEvent        adrsEvent, adrsReply;
  828.     OSErr            error, therror = noErr;
  829.     TargetID        senderID;
  830.  
  831. //    Send the player at address 'opponent' the message that player 'player'
  832. //    can be found at the address retrievable from addressID.
  833.         
  834.     AECreateAppleEvent (    kAbaloneEventClass,
  835.                             kSendAdrs,
  836.                             opponent,
  837.                             kAutoGenerateReturnID,
  838.                             kAnyTransactionID,
  839.                             & adrsEvent
  840.                         );
  841.  
  842. //    Try to extract the adress in a form it can be sent in to others.
  843.     
  844.     Assert (clientAddress->descriptorType == typeTargetID, INTERNAL_ERROR);
  845.     senderID = **(TargetID **)(clientAddress->dataHandle);
  846.  
  847. //    Add the player and his address as the parameters
  848.  
  849.     AEPutParamPtr (& adrsEvent, kSendPlyr, typePlyr, (Ptr) & player, sizeof (short));
  850.     AEPutParamPtr (& adrsEvent, kSendAdrs, typeAdrs, (Ptr) & senderID, sizeof (TargetID));
  851.     
  852. //    Send the message
  853.  
  854.     error = AESend    (    & adrsEvent,
  855.                         & adrsReply,
  856.                         kAENoReply,            //    Not in this case, thank you
  857.                         kAENormalPriority,
  858.                         kTimeOut,
  859.                         0L, //    (AEIdleProcPtr) IdleTime,
  860.                         0L
  861.                     );
  862.  
  863. //    Get the reply
  864.  
  865. //    Also when we don't want a reply?
  866.                     
  867. //    AEGetParamPtr    (    & adrsReply,
  868. //                        keyErrorNumber,
  869. //                        typeShortInteger,
  870. //                        & realType,
  871. //                        (Ptr) & therror,
  872. //                        sizeof (OSErr),
  873. //                        & realSize
  874. //                    );
  875.                             
  876. //    Dispose of created descriptor records
  877.     
  878.     AEDisposeDesc (& adrsEvent);
  879.     AEDisposeDesc (& adrsReply);
  880.     
  881.     return therror != noErr ? therror : error;
  882. }
  883.  
  884.  
  885.  
  886. pascal OSErr
  887. DoAEOpenApplication (AppleEvent message, AppleEvent reply, long refcon)
  888. {
  889.     OSErr    error = AEOpenApplicationHandler (& message, refcon);
  890.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  891.  
  892.     InitConnection();
  893.     
  894.     return error;
  895. }
  896.  
  897.  
  898.  
  899. pascal OSErr
  900. DoAEQuitApplication (AppleEvent message, AppleEvent reply, long refcon)
  901. {
  902.     OSErr    error = AEQuitApplicationHandler (& message, refcon);
  903.  
  904.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  905.  
  906.     return error;
  907. }
  908.  
  909.  
  910.  
  911. pascal OSErr
  912. DoAEOpenDocuments (AppleEvent message, AppleEvent reply, long refCon)
  913. {
  914.     OSErr    error = AEOpenDocumentsHandler (& message, refCon);
  915.  
  916.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  917.  
  918.     InitConnection();
  919.  
  920.     return error;
  921. }
  922.  
  923.  
  924.  
  925. pascal OSErr
  926. DoAEPrintDocuments (AppleEvent message, AppleEvent reply, long refCon)
  927. {
  928.     OSErr    error = AEPrintDocumentsHandler (& message, refCon);
  929.  
  930.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  931.  
  932.     return error;
  933. }
  934.  
  935.  
  936.  
  937. pascal OSErr
  938. DoAEMove (AppleEvent message, AppleEvent reply, long refCon)
  939. {
  940.     OSErr        error = AbaloneMoveHandler (& message, & reply, refCon);
  941.  
  942.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  943.  
  944.     return error;
  945. }
  946.  
  947.  
  948.  
  949. pascal OSErr
  950. DoAEGame (AppleEvent message, AppleEvent reply, long refCon)
  951. {
  952.     OSErr        error = AbaloneGameHandler (& message, & reply, refCon);
  953.  
  954.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  955.  
  956.     return error;
  957. }
  958.  
  959.  
  960.  
  961. pascal OSErr
  962. DoAEInit (AppleEvent message, AppleEvent reply, long refCon)
  963. {
  964.     OSErr        error = AbaloneInitHandler (& message, & reply, refCon);
  965.  
  966.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  967.  
  968.     return error;
  969. }
  970.  
  971.  
  972.  
  973. pascal OSErr
  974. DoAENew1 (AppleEvent message, AppleEvent reply, long refCon)
  975. {
  976.     OSErr        error = AbaloneNew1Handler (& message, & reply, refCon);
  977.  
  978.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  979.  
  980.     return error;
  981. }
  982.  
  983.  
  984.  
  985. pascal OSErr
  986. DoAEStop (AppleEvent message, AppleEvent reply, long refCon)
  987. {
  988.     OSErr        error = AbaloneStopHandler (& message, & reply, refCon);
  989.  
  990.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  991.  
  992.     return error;
  993. }
  994.  
  995.  
  996.  
  997. pascal OSErr
  998. DoAEAdrs (AppleEvent message, AppleEvent reply, long refCon)
  999. {
  1000.     OSErr        error = AbaloneAdrsHandler (& message, & reply, refCon);
  1001.  
  1002.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof (short));
  1003.  
  1004.     return error;
  1005. }
  1006.  
  1007.  
  1008.  
  1009. pascal OSErr
  1010. GotRequiredParams (AppleEvent *message)
  1011. {
  1012.     DescType realType;
  1013.     Size realSize;
  1014.     OSErr error = AEGetAttributePtr (message, keyMissedKeywordAttr, typeWildCard, & realType, nil, 0, & realSize);
  1015.  
  1016.     if (error == errAEDescNotFound)
  1017.         return noErr;
  1018.     
  1019.     return error == noErr ? errAEEventNotHandled : error;
  1020. }
  1021.